/*
 * Copyright 2014 Will Dignazio
 */
#include <curl/curl.h>
#include <socketio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include "internal.h"

int
sio_init(void)
{
	int ret;

	ret = curl_global_init(CURL_GLOBAL_ALL); // Initialize CURL library
	if(ret != 0)
		goto fail;

	global_workqueue = allocworkqueue();
	if(global_workqueue == NULL) { 
		ret = -1;
		goto fail;
	}

	ret = 0;
fail:
	return ret;
}

int
sio_connect(char *serv, SocketSession **session)
{
	SocketData *sdata;
	int ret;

	ret = -1;

	*session = allocsession();
	if(session == NULL)
		goto fail;

	sdata = allocdata(serv, sizeof(char), strlen(serv));
	if(sdata == NULL)
		goto fail;

       	ret = handshake(sdata, *session);
	return ret;
fail:
	freesession(*session);
	return ret;
}
